package com.iview;
import org.apache.log4j.Logger;

import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestDemo {
    private static Logger log = Logger.getLogger(TestDemo.class.getClass());
    /**
     * 直接执行 Python 语句
     */
//    public static void main(String[] args) {
//        PythonInterpreter interpreter = new PythonInterpreter();
//        interpreter.exec("a='Hello World!'; ");
//        interpreter.exec("print(a);");
//    }

    /**
     * 执行 py 文件
     */
//    public static void main(String[] args) {
//        PythonInterpreter interpreter = new PythonInterpreter();
//        interpreter.execfile("/Users/mingyang/Code/PythonCode/weixin-spider-1-master/a.py");
//
//        // 第一个参数为期望获得的函数（变量）的名字，第二个参数为期望返回的对象类型
//        PyFunction pyFunction = interpreter.get("sum", PyFunction.class);
//        int a = 2, b = 3;
//        //调用函数，传递 a, b 参数
//        PyObject result = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
//        System.out.println("this result is: " + result);
//    }

    /**
     *  Runtime.getRuntime() 调用
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */

    public static void main(String[] args) throws IOException, InterruptedException {
        // 设置命令行参数
        String[] commond = new String[]{"python3", "/Users/mingyang/Code/PythonCode/weixin-spider-1-master/a.py"};
        // 执行命令
        Process process = Runtime.getRuntime().exec(commond);
        // 获取程序执行或返回的结果
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            // 响应结果
            System.out.println("this is result: " + line);
        }
        in.close();
        // 返回值为 0 或 1，0 表示执行Python脚本成功 1 表示执行失败
        int resultCode = process.waitFor();
        System.out.println("执行状态: " + resultCode);

    }

    /**
     *  Runtime.getRuntime() 参数传递
     * @throws IOException
     * @throws InterruptedException
     */

//    public static void main(String[] args) throws IOException, InterruptedException {
//        // 设置命令行参数
//        String[] commond = new String[]{"python3", "/Users/mingyang/Code/PythonCode/weixin-spider-1-master/a.py", "5", "10"};
//        // 执行命令
//        Process process = Runtime.getRuntime().exec(commond);
//        // 获取程序执行或返回的结果
//        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
//        String line;
//        while ((line = in.readLine()) != null) {
//            // 响应结果
//            System.out.println("this is result: " + line);
//        }
//        in.close();
//        // 返回值为 0 或 1，0 表示执行Python脚本成功 1 表示执行失败
//        int resultCode = process.waitFor();
//        System.out.println("执行状态: " + resultCode);
//
//    }

    public static String getSign(String sign){
        log.info("sign被调用");
        return sign;
    }
}